#!/bin/sh

#Assemble files
nasm -f bin -o ./tmp/stage1.o -l ./tmp/stage1.lst ./src/bootloader/stage1.s
nasm -f bin -o ./tmp/LOADER.SYS -l ./tmp/loader.lst ./src/bootloader/loader.s

if [ ! -f ./tmp/stage1.o ]
then
	echo "ERROR: boot sector was not assembled."
	exit
fi

if [ ! -f ./tmp/LOADER.SYS ]
then
	echo "ERROR: stage 2 bootloader was not assembled."
	exit
fi

#Compile kernel
#cd ./src/kernel
#make
#cd ../..

#Recreate disk image. TODO: Next evolution will check for unchanged bootsector
rm ./img/disk.img
dd if=./tmp/stage1.o of=./img/disk.img
dd if=/dev/zero of=./img/disk.img bs=512 count=2879 oflag=append conv=notrunc

mcopy -i ./img/disk.img ./tmp/LOADER.SYS ::/

if [ -f ./src/kernel/KERNEL.SYS ]
then
	mcopy -i ./img/disk.img ./src/kernel/KERNEL.SYS ::/
else
	echo "WARNING: created floppy without kernel."
fi

echo "Floppy built."
